<style>
  .main-nav { display: none; }
  
</style>

<!-- TOPBAR S -->
<header class="top-bar flex-row">
  <span class="row-item row-item-auto top-bar-back" onclick="window.history.back()">返回</span>
  <h3 class="row-item top-bar-name">收货地址</h3>
  <span class="row-item row-item-auto top-bar-menu"></span>
</header>
<!-- TOPBAR E -->

<empty name="Think.get.buyUrl">

<!-- 收货地址的编辑 -->
<div id="deliver-address" class="deliver-address">

  <volist name="deliverAddressLists" id="vo">
  <section class="my mg10 transition">
    <div class="my-item my-address-item">
      <a class="link" href="__CONTROLLER__/modifyDeliverAddress/sid/{$sid}/id/{$vo.id}">
        <p><strong>{$vo.name}</strong>&nbsp;&nbsp;{$vo.tel}</p>
        <p>{$vo.address}</p>
        <a class="del btn btn-red transition" data-id="{$vo.id}">删除</a>
      </a>
    </div>
  </section>
  </volist>

  <section class="my mg10">
    <div class="my-item">
      <a class="link" href="__CONTROLLER__/addDeliverAddress/sid/{$sid}">添加新地址</a>
    </div>
  </section>

</div>
<script type="text/javascript">
  //删除地址功能能，左滑出现删除按钮，右滑消失，点击跳转。
  (function(){
    var addressItem = document.querySelectorAll ( "#deliver-address .my-address-item" ),
        delButtons = document.querySelectorAll ( "#deliver-address .del" );
    for ( var i=0; item=addressItem[i]; i++ ) {
      item.addEventListener ( "touchstart", bindTouchStart, false );
      item.addEventListener ( "touchmove", bindTouchMove, false );
      item.addEventListener ( "touchend", bindTouchEnd, false );
    };
    for ( var i=0; item=delButtons[i]; i++ ) {
      item.addEventListener ( "touchend", delAddress, false );
    };
    var startX = 0,
        startY = 0;
    function bindTouchStart ( e ) {
    	e.preventDefault();
      var touch = e.touches[0];
      startX = touch.pageX;
      startY = touch.pageY;
    };
    function bindTouchMove ( e ) {
    	e.preventDefault();
      var touch = e.touches[0];
      var x = touch.pageX - startX,
          y = touch.pageY - startY;
      if ( x < -30 && y < 70 ) {
        this.querySelector ( ".del" ).style.WebkitTransform = "translate(-10.5em, -50%)";
      } else if ( x > 30 && y < 70 ) {
    	  this.querySelector ( ".del" ).style.WebkitTransform = "translate(10.5em, -50%)";
      };
    };
    function bindTouchEnd ( e ) {
    	var touch = e.changedTouches[0];
      var x = touch.pageX - startX,
          y = touch.pageY - startY;
      if ( x == 0 & y == 0 ) {
    	  location.href = this.children[0].getAttribute ( "href" );
      }
    };

    function delAddress ( e ) {
      e.stopPropagation();
      var _this = this;
      modal.confirm({
        title: "地址删除",
        text: "删除后无法撤消，确认要删除吗？",
        buttons: [{
          text: "取消",
          close: true
        }, {
          text: "确认",
          close: true,
          click: function() {
            del.call(_this);
          }
        }]
      });
    };

    function del() {

      var id = this.getAttribute ( "data-id" ),
          sid = {$sid},
          _this = this;

      GH.ajax ( "__CONTROLLER__/deleteDeliverAddress", {"id": id, "sid": sid}, function(data){
        GH.showMessage ( data.msg );
        if ( data.error == 0 ) {
          _this.parentNode.parentNode.style.height = "0";
          setTimeout( function(){
            _this.parentNode.parentNode.remove();
          }, 1000 );
        }
      }, "post" );
    };
  })();
</script>

<else/>

<!-- 收货地址的选择 -->
<div id="deliver-address" class="deliver-address pd10">
  
  <volist name="deliverAddressLists" id="vo">
  <section class="address-item pd10 mb10{$vo['status']?' active':''}" data-address-id="{$vo.id}">
    <p><strong>{$vo.name}</strong>&nbsp;&nbsp;{$vo.tel}</p>
    <p>{$vo.address}</p>
  </section>
  </volist>

  <a class="btn btn-brown w100 mb10" href="__CONTROLLER__/addDeliverAddress/sid/{$sid}?buyUrl={$buyUrl}">新增收货地址</a>

</div>
<script type="text/javascript">
  (function(){
   var oAddress = document.getElementById ( "deliver-address" ),
       buyUrl = "{$_GET['buyUrl']}",
       sid = {$sid};
   GH.delegate( "click", oAddress, "section", function(){
    deliverId = this.getAttribute ( "data-address-id" );
    GH.ajax ( "__CONTROLLER__/updateAddressStatus", { "deliverId": deliverId, "sid" : sid }, function(data){
     ( data.error == 0 ) && ( window.location = buyUrl );
    }, "post" );
   });
  })()
</script>

</empty>


